/*
 * Copyright 2012 Andrea Parodi
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package so.eban.JUnadvisable;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

public final class UnadvisableMode extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setupCommonButtonListeners();
        setupBeepOn5();

    }

    /*@Override public boolean dispatchTouchEvent(MotionEvent ev) {
        System.out.println(ev);
        return super.dispatchTouchEvent(ev);
    }*/

    private final Object beepLock=new Object();

    private long lastClick;

    private void setupBeepOn5() {
        Button centralButton = (Button)findViewById(R.id.btn5);
        centralButton.setOnTouchListener(new View.OnTouchListener() {
            @Override public boolean onTouch(View v, MotionEvent event) {
               if (event.getAction()==MotionEvent.ACTION_DOWN){

                   synchronized (beepLock){
                       if (lastClick==0 || System.currentTimeMillis()-lastClick>1000){
                           final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100);
                           tg.startTone(ToneGenerator.TONE_PROP_BEEP2);
                           lastClick=System.currentTimeMillis();
                       }

                   }

                   return true;
               }

                return false;
            }
        });
    }

    private void setupCommonButtonListeners() {
        int[] viewIds = new int[] {
                R.id.btn1,
                R.id.btn2,
                R.id.btn3,
                R.id.btn4,
                R.id.btn5,
                R.id.btn6,
                R.id.btn7,
                R.id.btn8,
                R.id.btn9
        };

        for (int id:viewIds)
        {
            Button button = (Button)findViewById(id);
            button.setOnTouchListener(new View.OnTouchListener() {
                @Override public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction()==MotionEvent.ACTION_MOVE){
                        System.out.println("Vrrrrrrrrrrrrrr");
                        // Get instance of Vibrator from current Context
                        Vibrator vbr = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

                        // Vibrate for 300 milliseconds
                        vbr.vibrate(300);
                        return true;
                    }
                    return false;
                }
            });


        }
    }
}
